<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>优惠券列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/public/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/public/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">

                        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                            <div class="layui-form-item">

                                <div class="layui-inline">
                                    <label class="layui-form-label">优惠券名称</label>
                                    <div class="layui-input-block" style="width: 300px;">
                                        <input class="layui-input" name="name" placeholder="请输入优惠券名称" autocomplete="off">
                                    </div>
                                </div>

                                <div class="layui-inline layui-form">
                                    <label class="layui-form-label">优惠券类型</label>
                                    <div class="layui-input-block" style="width: 200px;">
                                        <select name="type">
                                            <option value="">全部类型</option>
                                            <option value="0">免费券</option>
                                            <option value="1">现金券</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline layui-form">
                                    <label class="layui-form-label">优惠券场景</label>
                                    <div class="layui-input-block" style="width: 200px;">
                                        <select name="use">
                                            <option value="">全部场景</option>
                                            <option value="0">自用</option>
                                            <option value="1">赠送</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-search">
                                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"> 搜索</i>
                                    </button>
                                </div>
                            </div>
                        </div>


                        <table class="layui-hide" id="test-table-reload" lay-filter="test-table-reload"></table>
                        <script type="text/html" id="test-table-reload-toolbar">
                            <button class="layui-btn layui-btn-sm #{if !toolsUtils.checkBtn('/coupon/add')} layui-hide #{/if}" lay-event="addCoupon"><i class="layui-icon layui-icon-add-circle-fine"></i>添加优惠券</button>
                        </script>

                        <script type="text/html" id="test-table-reload-barDemo">
                            <a class="layui-btn layui-btn-xs #{if !toolsUtils.checkBtn('/coupon/edit')} layui-hide #{/if}" lay-event="edit" ><i class="layui-icon layui-icon-edit"></i> {{# if(d.invalid===1){ }} 查看 {{# } else { }}编辑{{# } }}</a>
                            <a class="layui-btn layui-btn-xs #{if !toolsUtils.checkBtn('/user/selectUser')} layui-hide #{/if}" lay-event="dispatch" {{# if(d.invalid===1){ }} style='display: none' {{# } }}><i class="layui-icon layui-icon-edit"></i>派发优惠券</a>
                            <a class="layui-btn layui-btn-xs #{if !toolsUtils.checkBtn('/coupon/receive')} layui-hide #{/if}" lay-event="dispatchList"><i class="layui-icon layui-icon-edit"></i>派发记录</a>
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/public/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/public/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table','utils', 'form'], function(){
        var $ = layui.$,
                table = layui.table,
                form = layui.form,
                admin = layui.admin;

        //监听搜索
        form.on("submit(LAY-search)", function (data) {
            var field = data.field;

            //执行重载
            table.reload('test-table-reload', {
                where: field
            });
        });

        //方法级渲染
        table.render({
            elem: '#test-table-reload'
            ,url: "/coupon/getCouponList"
            ,toolbar: '#test-table-reload-toolbar'
            ,defaultToolbar:['filter']
            ,cellMinWidth: 80
            ,cols: [[
                {field:'id', title: 'ID',fixed: true}
                , {field:'name', title: '优惠券名称',width:200}
                , {field:'typeName', title: '类型'}
                , {field:'useTypeName', title: '场景'}
                , {field:'type', title: '优惠内容',templet:function (d) {
                        if(d.type==0){
                             return "<div>免费</div>";
                        }else if(d.type==1){
                            return "<div>"+d.val+"元</div>";
                        }
                    }}
                , {field:'couponStatusName', title: '状态'}
                , {field:'totalCount', title: '总数'}
                , {field:'dispatchCount', title: '已领取'}
                , {field:'remainCount', title: '剩余'}
                , {field:'usedCount', title: '已使用'}
                , {field:'createByName', title: '创建人',width:100}
                , {field:'dispatchTypeName', title: '派发方式',width:100}
                , {field:'createTime', title: '创建时间',width:200,sort: true,templet: function (d) {
                        return "<div>"+layui.toDateString(d.createTime)+"</div>";
                    }}
                , {fixed: 'right', title:'操作', toolbar: '#test-table-reload-barDemo', width:280}
            ]]
            ,page: true
            ,height: 524
        });

        //头工具栏事件
        table.on('toolbar(test-table-reload)', function(obj){
            if(obj.event === 'addCoupon'){
                window.location.href="/coupon/add";
            }
        });

        //监听行工具事件
        table.on('tool(test-table-reload)', function(obj){
            var data = obj.data;
            var id = data.id;

            if(obj.event === 'edit'){
                window.location.href="/coupon/edit?id="+data.id;
            } else if(obj.event === 'dispatch'){
                layer.open({
                    type: 2
                    ,title: '选择用户'
                    ,content: '/user/selectUser'
                    ,area: ['900px', '700px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submit = layero.find('iframe').contents().find("#addUserIdsSubmit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(addUserIdsSubmit)', function(data){
                            var field = data.field; //获取提交的字段

                            var pars={"couponId":id,"userIds":field.userIds};

                            $.post("/coupon/saveDispatchUser", pars, function (result) {
                                if (result.code > 0) {
                                    layer.msg(result.msg);
                                    return false;
                                }else{
                                    layer.msg("发放优惠券成功");
                                    window.location.reload();
                                }
                            });
                            layer.close(index); //关闭弹层

                        });
                        submit.trigger('click');
                    }
                });
            }else if(obj.event === 'dispatchList'){
                window.location.href="/coupon/receive?id="+data.id;
            }
        });

    });
</script>
</body>
</html>